package service

import (
	"errors"
	"strings"

	"zhonghui/console/warehouse/dbx/db_zh_kernel_slave"
	"zhonghui/console/warehouse/envx"
	"zhonghui/console/warehouse/helperx"
	"zhonghui/console/warehouse/modelx/model_zh_kernel"
)

// GenLocalSmsUrl 生成短信用的本地短网址
func GenLocalSmsUrl(recordID uint64, testPhone, p string) string {
	h, _ := helperx.HashIdEncode(int(recordID))

	if testPhone != "" {
		h = "TTSS_" + h + "_" + testPhone
	}

	// 转成本地短网址
	localDwz := ToLocalDwz(p, h)
	// logx.Zap().Infow("生成local短网址", "localDwz", localDwz)

	return localDwz
}

func SendSms(phone string, param []string, isQueue bool, template string) error {
	// mock.MockSendSms()
	// return nil

	// // todo 暂时不发队列里的
	// if isQueue {
	// 	return nil
	// }

	// return nil

	if template == "1b71f6678fc44defbcb2cbefb0aa8f6c" || template == "160ae717a1fc46fcbba54a93180517be" || template == "6cac1f8f6ad64c5bbbc45ad72c2716b9" {
		param = param[1:2]
	}

	// 非生产环境，且是队列，不发
	if !envx.InProd() && isQueue {
		return nil
	}

	if strings.HasPrefix(template, "JD_") {
		tp := model_zh_kernel.SysSmsTemplate{}
		db_zh_kernel_slave.Client().Where("template_id = ?", template).Take(&tp)

		suc, _ := SendZhongHuiJDSms(phone, param, tp.Content)
		if suc {
			return nil
		}
		return errors.New("jd短信发送失败")
	}

	return SendZhonghuiSms(phone, param, template)
}

func HasDwz(content string) bool {
	return strings.Contains(content, "dwz.cn") || strings.Contains(content, "j.pubmi.cn")
}
